package com.example.personal.mvvm.activity

import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.personal.R
import com.example.personal.mvvm.viewmodel.SecretViewModel
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.core.RouterConstants.MODIFY_SECRET_ACTIVITY
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.manager.UserManager
import com.gsafety.libraryktx.util.AppUtil
import kotlinx.android.synthetic.main.personal_modify_secret_activity.*

@Route(path = MODIFY_SECRET_ACTIVITY)
class ModifySecretActivity : BaseActivity(),View.OnClickListener {
    private lateinit var mv: SecretViewModel
    override fun initView() {
        var title = findViewById<TextView>(R.id.title_name)
        title.text = resources.getText(R.string.personal_my_secret)
        initmodel()
    }

    override fun initData() {
        bt_sure.setOnClickListener(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.personal_modify_secret_activity
    }



    /**
     * 初始化viewmodel
     */
    private fun initmodel() {
        mv = SecretViewModel()
        mv = create(SecretViewModel::class)
        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Success -> {
                    disDia()
                    toast("修改成功,请重新登录")
                    AppUtil.exitLogin()
                }
                is LoadState.Fail -> {
                    disDia()
                    toast(it.msg)
                }
            }
        })
    }

    //逻辑判断
    fun judge(): Boolean {
        var old = et_now.text.toString().trim()
        var new = et_new.text.toString().trim()
        var newAgain = et_confirm.text.toString().trim()
        if (old.isEmpty()) {
            toast("请输入旧密码！")
            return false
        }

        var readPwd = UserManager.getInstance().getUser()!!.passWord!!
        if (new == old) {
            toast("输入的新密码和旧密码不能一样！")
            return false
        }

        if (new.isEmpty()) {
            toast("请输入新密码！")
            return false
        }
        if (6 > new.length) {
            toast("密码不能小于6位！")
            return false
        }
        if (16 < new.length) {
            toast("密码不能大于16位！")
            return false
        }

        if (newAgain.isEmpty()) {
            toast("请再次输入新密码！")
            return false
        }
        if (newAgain != new) {
            toast("前后新密码不一致，请重新输入！") //新密码不符合规则！
            return false
        }
        if (readPwd != old) {
            toast("旧密码输入错误，请重新输入！")
            return false
        }
        return true

    }

    override fun onClick(v: View) {
        when(v.id){
            R.id.bt_sure->{
                if (judge()) {
                    var old = et_now.text.toString().trim()
                    var new = et_new.text.toString().trim()
                    showDia("")
                    mv.updatePassword(old,new)


                }
            }
        }
    }

}